package com.junjie.checkin.websocket.config;

import com.junjie.checkin.websocket.handler.CustomPrincipalHandshakeHandler;
import com.junjie.checkin.websocket.interceptor.AuthHandshakeInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * @author 洪浚杰
 * @description
 * @date 2025/6/23 09:15
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebsocketConfig implements WebSocketMessageBrokerConfigurer {

    @Autowired
     AuthHandshakeInterceptor authHandshakeInterceptor;

    @Autowired
    CustomPrincipalHandshakeHandler customPrincipalHandshakeHandler;

    // 注册端点，可以注册多个
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws")
                .setAllowedOriginPatterns("*")
                .setHandshakeHandler(customPrincipalHandshakeHandler)
//                .withSockJS();
                .addInterceptors(authHandshakeInterceptor);  //添加拦截器如果有必要
    }
    // 配置消息代理
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic","/queue","/user"); //配置消息代理
        registry.setApplicationDestinationPrefixes("/app");  // 应用目的地前缀
        registry.setUserDestinationPrefix("/user");  // 用户目的地前缀
    }


}
